package com.example.demo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import com.example.demo.dto.NewTaskFlowDTO;
import com.example.demo.dto.PageExtra;
import com.example.demo.request.CreateNewTaskFlowRequest;
import com.example.demo.request.BatchAssignTaskFlowRequest;
import com.example.demo.response.ApiResponse;
import com.example.demo.service.NewTaskFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/new-task-flows")
public class NewTaskFlowController {

        @Autowired
        private NewTaskFlowService newTaskFlowService;
        @GetMapping
        public ResponseEntity<ApiResponse<List<NewTaskFlowDTO>>> getNewTaskFlows(
                       @RequestParam(defaultValue = "0") int page,
                       @RequestParam(defaultValue = "10") int size,
                        @RequestParam(required = false) Long taskId,
                         @RequestParam(required = false) Long flowId,
                        HttpServletRequest request) {
                List<NewTaskFlowDTO> newTaskFlows = newTaskFlowService.getNewTaskFlows(page, size, taskId, flowId);
                long total = newTaskFlowService.countNewTaskFlows(taskId, flowId);
                PageExtra pageExtra = new PageExtra(page, size, total);

                return ResponseEntity.ok(
                                ApiResponse.success(newTaskFlows, "查询成功", request.getRequestURI(), pageExtra));
        }


        @GetMapping("/{id}")
        public ResponseEntity<ApiResponse<NewTaskFlowDTO>> getNewTaskFlowById(
                        @PathVariable Long id,
                        HttpServletRequest request) {
                NewTaskFlowDTO newTaskFlowDTO = newTaskFlowService.getNewTaskFlowById(id);
                return ResponseEntity.ok(
                                ApiResponse.success(newTaskFlowDTO, "查询成功", request.getRequestURI(), null));
        }


        @GetMapping("/task/{taskId}/flow/{flowId}")
        public ResponseEntity<ApiResponse<NewTaskFlowDTO>> getNewTaskFlowByTaskIdAndFlowId(
                       @PathVariable Long taskId,
                        @PathVariable Long flowId,
                        HttpServletRequest request) {
                NewTaskFlowDTO newTaskFlowDTO = newTaskFlowService.getNewTaskFlowByTaskIdAndFlowId(taskId, flowId);
                return ResponseEntity.ok(
                                ApiResponse.success(newTaskFlowDTO, "查询成功", request.getRequestURI(), null));
        }


        @GetMapping("/task/{taskId}")
        public ResponseEntity<ApiResponse<List<NewTaskFlowDTO>>> getNewTaskFlowsByTaskId(
                       @PathVariable Long taskId,
                        HttpServletRequest request) {
                List<NewTaskFlowDTO> newTaskFlowDTOs = newTaskFlowService.getNewTaskFlowsByTaskId(taskId);
                return ResponseEntity.ok(
                                ApiResponse.success(newTaskFlowDTOs, "查询成功", request.getRequestURI(), null));
        }


        @GetMapping("/flow/{flowId}")
        public ResponseEntity<ApiResponse<List<NewTaskFlowDTO>>> getNewTaskFlowsByFlowId(
                        @PathVariable Long flowId,
                        HttpServletRequest request) {
                List<NewTaskFlowDTO> newTaskFlowDTOs = newTaskFlowService.getNewTaskFlowsByFlowId(flowId);
                return ResponseEntity.ok(
                                ApiResponse.success(newTaskFlowDTOs, "查询成功", request.getRequestURI(), null));
        }


        @PostMapping
        public ResponseEntity<ApiResponse<NewTaskFlowDTO>> createNewTaskFlow(
                        @Valid @RequestBody CreateNewTaskFlowRequest request,
                        HttpServletRequest httpRequest) {
                NewTaskFlowDTO newTaskFlowDTO = newTaskFlowService.createNewTaskFlow(request);
                return ResponseEntity.ok(
                                ApiResponse.created(newTaskFlowDTO, "创建成功", httpRequest.getRequestURI()));
        }

        @PutMapping("/{id}")
        public ResponseEntity<ApiResponse<NewTaskFlowDTO>> updateNewTaskFlow(
                       @PathVariable Long id,
                        @Valid @RequestBody CreateNewTaskFlowRequest request,
                        HttpServletRequest httpRequest) {
                NewTaskFlowDTO newTaskFlowDTO = newTaskFlowService.updateNewTaskFlow(id, request);
                return ResponseEntity.ok(
                                ApiResponse.success(newTaskFlowDTO, "更新成功", httpRequest.getRequestURI(), null));
        }


        @DeleteMapping("/{id}")
        public ResponseEntity<ApiResponse<Void>> deleteNewTaskFlow(
                     @PathVariable Long id,
                        HttpServletRequest request) {
                newTaskFlowService.deleteNewTaskFlow(id);
                return ResponseEntity.ok(
                                ApiResponse.success(null, "删除成功", request.getRequestURI(), null));
        }


        @PostMapping("/batch-assign")
        public ResponseEntity<ApiResponse<Integer>> batchAssignTasksToFlow(
                        @Valid @RequestBody BatchAssignTaskFlowRequest request,
                        HttpServletRequest httpRequest) {
                int assignedCount = newTaskFlowService.batchAssignTasksToFlow(
                                request.getDataSourceId(), request.getProcessId());
                return ResponseEntity.ok(
                                ApiResponse.success(assignedCount,
                                                "批量分配成功，共分配" + assignedCount + "条任务",
                                                httpRequest.getRequestURI(), null));
        }


        @GetMapping("/undistributed/datasource/{dataSourceId}")
        public ResponseEntity<ApiResponse<List<NewTaskFlowDTO>>> getUndistributedTaskFlowsByDataSourceId(
                        @PathVariable Long dataSourceId,
                        HttpServletRequest request) {
                List<NewTaskFlowDTO> undistributedTaskFlows = newTaskFlowService
                                .getUndistributedTaskFlowsByDataSourceId(dataSourceId);
                return ResponseEntity.ok(
                                ApiResponse.success(undistributedTaskFlows, "查询成功", request.getRequestURI(), null));
        }


        @GetMapping("/undistributed/flow/{flowId}")
        public ResponseEntity<ApiResponse<List<NewTaskFlowDTO>>> getUndistributedTaskFlowsByFlowId(
                       @PathVariable Long flowId,
                        HttpServletRequest request) {
                List<NewTaskFlowDTO> undistributedTaskFlows = newTaskFlowService
                                .getUndistributedTaskFlowsByFlowId(flowId);
                return ResponseEntity.ok(
                                ApiResponse.success(undistributedTaskFlows, "查询成功", request.getRequestURI(), null));
        }

        @PostMapping("/batch-distribute/datasource/{dataSourceId}")
        public ResponseEntity<ApiResponse<Integer>> batchDistributeTaskFlows(
                     @PathVariable Long dataSourceId,
                        HttpServletRequest httpRequest) {
                int distributedCount = newTaskFlowService.batchDistributeTaskFlows(dataSourceId);
                return ResponseEntity.ok(
                                ApiResponse.success(distributedCount,
                                                "批量分发成功，共分发" + distributedCount + "条任务流程关系",
                                                httpRequest.getRequestURI(), null));
        }

//        @Operation(summary = "根据流程ID批量分发任务流程关系", description = "获取指定流程的未分发任务流程关系，并将其标记为已分发")
        @PostMapping("/batch-distribute/flow/{flowId}")
        public ResponseEntity<ApiResponse<Integer>> batchDistributeTaskFlowsByFlowId(
                         @PathVariable Long flowId,
                        HttpServletRequest httpRequest) {
                int distributedCount = newTaskFlowService.batchDistributeTaskFlowsByFlowId(flowId);
                return ResponseEntity.ok(
                                ApiResponse.success(distributedCount,
                                                "批量分发成功，共分发" + distributedCount + "条任务流程关系",
                                                httpRequest.getRequestURI(), null));
        }
}